Généralités

Imports

In [2]:
import numpy as np
import matplotlib.pyplot as plt

On récupére les deux modules de bases pour le calcul scientifique :

  • numpy (numerical python) permettant la gestion de grand tableaux de nombres de manière efficace
  • matplotlib.pyplot qui permet d'obtenir un affichage graphique

On notera la syntaxe :
import nom du module as nom qu'on utilisera pour l'appeler

Comment obtenir l'inscrustation des graphiques dans le notebook?

In [3]:
%matplotlib inline

Comment redéfinir la taille par défaut des graphes?

In [4]:
plt.rc('figure',figsize=(12,9))

Comment obtenir de l'aide sur une fonction?

In [5]:
plt.rc?

Utilisation de la touche tabulation

  • la touche de tabulation (TAB) effectue l'autcomplétion s'il n'y a qu'une possibilité, et ouvre un menu avec tous les choix possibles sinon.
  • la combinaison SHIFT-TAB permet d'obtenir de l'aide sur ce qui précède.

Comment taper du LaTeX

Il suffit de taper le code LaTeX dans une cellule markdown \begin{equation} \sum_{k=1}^n{\frac{1}{k^2}}=\frac{\pi^2}{6} \end{equation}

Premiers Affichages

Comment tracer le graphe d'une fonction le plus simplement possible?

In [6]:
x=np.linspace(-5.0,5.0,500)
plt.plot(x, np.sin(2*x))
Out[6]:
[<matplotlib.lines.Line2D at 0x7f4030d66160>]

Comment afficher une courbe paramétrée.

In [7]:
t=np.linspace(-np.pi, np.pi, 501)
x=np.sin(4*t)
y=np.sin(3*t)
plt.plot(x, y, lw=2.0)
Out[7]:
[<matplotlib.lines.Line2D at 0x7f4030d3de80>]

Comment obtenir un histogramme pour une série de nombres.

In [8]:
donnees = np.random.random(1000)
plt.hist(donnees, bins=10, normed=True)
Out[8]:
(array([ 0.97144179,  0.93138233,  1.0515607 ,  0.92136747,  0.92136747,
         0.96142692,  1.10163501,  0.99147151,  1.10163501,  1.06157556]),
 array([ 0.00135951,  0.10121109,  0.20106267,  0.30091425,  0.40076584,
         0.50061742,  0.600469  ,  0.70032059,  0.80017217,  0.90002375,
         0.99987533]),
 <a list of 10 Patch objects>)

Comment obtenir une heatmap.

In [9]:
x=np.linspace(-1.0, 1.0, 101)
y=np.linspace(-1.0, 1.0, 101)
xx, yy = np.meshgrid(x,y)
zz=np.cos(5*np.pi*xx)*np.sin(3*np.pi*yy)
plt.imshow(zz, extent=(-1.0, 1.0, -1.0, 1.0), cmap=plt.cm.viridis)
plt.colorbar()
Out[9]:
<matplotlib.colorbar.Colorbar at 0x7f4030c08898>

Comment obtenir des courbes de niveaux.

In [10]:
x=np.linspace(-1.0, 1.0, 101)
y=np.linspace(-1.0, 1.0, 101)
xx, yy = np.meshgrid(x,y)
zz=np.cos(5*np.pi*xx)*np.sin(3*np.pi*yy)*np.exp(-(xx**2+yy**2))
plt.contour(xx, yy, zz, np.arange(zz.min(), zz.max(), 0.1), cmap=plt.cm.magma)
plt.colorbar()
plt.title('$z=\sin(5\pi x) \sin(3\pi y)$')
Out[10]:
<matplotlib.text.Text at 0x7f4030b59c50>

Affichages plus avancés

In [13]:
f, ax = plt.subplots(1, 2)
ax1, ax2=ax
In [14]:
x=np.linspace(-np.pi, np.pi, 201)
ligne1,=ax1.plot(x, np.sin(3*x)*np.cos(4*x), lw=2, color="blue", label="Graphe fonction")
In [15]:
y=np.linspace(-2*np.pi, 2*np.pi, 401)
ligne2,=ax2.plot(x, np.exp(-x**2)*np.cos(x+x**2), lw=2, color='red', label="Autre Graphe Fonction")
In [16]:
f
Out[16]:
In [17]:
ax1.legend(loc="best")
f
Out[17]:
In [18]:
ligne1.set_ydata(np.cos(x-x**2))
f
Out[18]: